const Category = require('../models/index').getModel('category')

const category = {
    // 获取所有分类
    async getAllCategory() {
        return Category.find()
    },
    // 删除分类
    async deleteCategory(categoryList) {
        return Category.deleteMany(categoryList)
    },
    // 新增分类
    async createCategory(categoryList) {
        console.log('createCategory service', categoryList);
        return Category.insertMany(categoryList)
    },
    // 修改一个分类
    async updateOneCategory(id, update) {
        return Category.findByIdAndUpdate(id, update, {new: true})
    },

    async updateArticleCountUnderCategory(record) {
        record.map(async (r) => {
            let temp = await Category.findOneAndUpdate({name: r._id}, {articleCount: r.articleCount})
            // console.log(temp)
        })
    }
}

module.exports = category
